package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.smallgame.SmallGameConfig;
import com.yanqu.road.entity.config.smallgame.SmallGameType;
import com.yanqu.road.logic.bussiness.config.SmallGameConfigBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.smallgame.stage.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SmallGameConfigMgr extends TempMgr {

    private static Map<Integer, Map<Integer, SmallGameConfig>> smallGameConfigMap = new HashMap<>();
    private static Map<Integer, Class<?>> stageMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        smallGameConfigMap = SmallGameConfigBusiness.getSmallGameConfigList();
        loadGameType();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static SmallGameConfig getSmallGameBaseConfig(int type, int id) {
        if (null == smallGameConfigMap.get(type)) {
            return null;
        }
        return smallGameConfigMap.get(type).get(id);
    }

    public static boolean loadGameType() {
        stageMap.put(SmallGameType.GridSwitch, GridSwitchStage.class);
        stageMap.put(SmallGameType.RoadCombine, RoadCombineStage.class);
        stageMap.put(SmallGameType.DrawLineDefend, DrawLineDefendStage.class);
        stageMap.put(SmallGameType.KeySwitch, KeySwitchStage.class);
        stageMap.put(SmallGameType.WaterSort, WaterSortStage.class);
        return true;
    }

    public static SmallGameStageInterface getStage(SmallGameConfig config) {
        if (!stageMap.containsKey(config.getType())) {
            return null;
        }
        try {
            SmallGameStageInterface stage = (SmallGameStageInterface) stageMap.get(config.getType()).newInstance();
            return stage.initByConfig(config);
        } catch (Exception e) {
            return null;
        }
    }
}
